<template>
	<view class="personnel-inspection-container">
		<view class="common-btns-container">
			<view class="common-btn-container" @tap="rosterInspection" hover-class="common-btn">人脸识别</view>
		</view>
	</view>
</template>

<script>
import { common } from '@/pagesA/utils/common.js';
const app = getApp();
export default {
	onLoad() {
		common.init();
	},
	onShow() {},
	methods: {
		personnelInspection(faceImage) {
			//人员巡检
			common.apis.home
				.personnelInspection({
					faceImage
				})
				.then(
					(res) => {
						if (res.data) {
							common.message.toast('识别成功');
							uni.redirectTo({
								url: `/pagesA/pages/roster/detail/detail?uuid=${res.data.uuid}&projectUuid=${res.data.projectUuid}`
							});
						} else {
							common.message.toast('人员巡检识别为空');
						}
					},
					(err) => {
						uni.hideLoading();
						uni.showModal({
							title: '温馨提示',
							content: err.msg,
							showCancel: false
						});
					}
				);
		},
		rosterInspection() {
			//人员巡检
			app.globalData.isPreview = true;
			uni.chooseMedia({
				count: 1,
				mediaType: ['image'],
				sizeType: ['compressed'],
				success: (res) => {
					const tempFiles = res.tempFiles;
					const fileManager = uni.getFileSystemManager();
					fileManager.readFile({
						filePath: tempFiles[0].tempFilePath,
						encoding: 'base64',
						success: (fileRes) => {
							this.personnelInspection(fileRes.data);
						},
						fail: (fileErr) => {
							uni.showModal({
								title: '温馨提示',
								content: '读取图片失败，' + JSON.stringify(fileErr),
								showCancel: false
							});
						}
					});
				},
				fail: (err) => {
					if (JSON.stringify(err).indexOf('chooseMedia:fail cancel') < 0) {
						uni.showModal({
							title: '温馨提示',
							content: '获取图片失败，' + JSON.stringify(err),
							showCancel: false
						});
					}
				}
			});
		}
	}
};
</script>

<style>
@import url('../../assets/style/common.css');
.personnel-inspection-container {
	display: flex;
	flex-direction: column;
	height: 100vh;
}
</style>
